Vue.component('config-log', {//修改记录组件
    template: `<el-dialog title="修改记录" :visible.sync="visibleDialog" width="80vw">
    <el-table :data="tableData" stripe border height="50vh" style="width: 100%">
        <el-table-column align="center" type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="Gift_Points" label="下单后每1元赠送多少积分给客户"></el-table-column>
        <el-table-column prop="Points_Deduction" label="满足多少元可以抵扣积分"></el-table-column>
        <el-table-column prop="Deduction_Ratio" label="积分抵扣金额比例"></el-table-column>
        <el-table-column prop="Deduction_Limit" label="每笔订单最多抵扣多少积分"></el-table-column>
        <el-table-column prop="Row_Status" label="每笔订单最多抵扣多少积分"></el-table-column>
        <el-table-column prop="Operation_Datetime" label="操作时间"></el-table-column>
    </el-table>
    <el-pagination style="margin-top: 5px;" background @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="form.page" :page-sizes="[10, 20, 30, 50]"
        :page-size="form.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:visible',false)">取消</el-button>
        <el-button type="primary" @click="submitForm('contract')">提交</el-button>
    </span></el-dialog>`,
    props: ['refresh', 'visible'],
    data() {
        return {
            visibleDialog: false,
            refreshDialog: false,
            tableData: [],
            form: {
                SQLSerialNumber: "P_business_points_config",
                PaginationNumber: 1,
                RowAmount: 10,
                ExtensionCondition: '',
                Condition: ''
            },
            total: 0,
        }
    },
    watch: {
        visible() {
            this.visibleDialog = this.visible
        },
        refresh(val) {
            this.refreshDialog = this.refresh
        },
        visibleDialog(val) {
            this.$emit('update:visible', val)
        },
        refreshDialog(val) {
            if (val) {
                this.getList(this.form);
            }
            this.$emit('update:refresh', val);
        }
    },
    mounted() {
        this.getList(this.form);
    },
    methods: {
        handleSizeChange(val) {
            this.form.PaginationNumber = 1;
            this.form.RowAmount = val;
            this.getList(this.form)
        },
        handleCurrentChange(val) {
            this.form.PaginationNumber = val;
            this.getList(this.form)
        },
        getList(data) {
            const that = this
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
                type: 'POST',
                data,
                success: function (Result) {
                    that.tableData = Result.Rows;
                    that.total = Result.TotalRows;
                },
                complete: function (XMLHttpRequest) {
                    that.refreshDialog = false;
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        }
    }
})
new Vue({
    el: '#app',
    data() {
        return {
            form: {
                Points_Config_SerialNumber: '',
                Gift_Points: 0,//下单后每1元赠送多少积分给客户
                Points_Deduction: 1,//满足多少元可以抵扣积分
                Deduction_Ratio: 1,//抵扣比例
                Deduction_Limit: 300000,//上限
                Enterprise_Info_SerialNumber: '',
                Row_Status: '1'//0开启 1关闭
            },
            page: {
                SQLSerialNumber: "P_business_points_detail_config",
                PaginationNumber: 1,//页数
                RowAmount: 10,//每页数据条数
                Article_Name: '',
                Supplier_SerialNumber: '',
                Brand_SerialNumber: '',
                ExtensionCondition: '',
                Condition: ''
            },
            total: 0,
            tableData: [],
            supplier: [],
            brand: [],
            loading: false,
            OldForm: null,
            rules: {},
            activeNames: ['1'],
            dialogVisible: false,
            Refresh: false,
        }
    },
    mounted() {
        this.getData();
        this.getList(this.page);
    },
    methods: {
        onSerach() {			//查询方法
            this.form.PaginationNumber = 1;
            this.getList(this.page)
        },
        handleSizeChange(val) {
            this.page.PaginationNumber = 1;
            this.page.RowAmount = val;
            this.getList(this.page)
        },
        handleCurrentChange(val) {
            this.page.PaginationNumber = val;
            this.getList(this.page)
        },
        getData() {
            const that = this;
            const Condition = "Row_Status='0'"
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_brand',
                    Condition,
                },
                success: function (res) {
                    that.brand = res;
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_supplier_info',
                    Condition,
                },
                success: function (res) {
                    that.supplier = res;
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: 'POST',
                data: {
                    SQLSerialNumber: 'C_business_points_config',
                    Condition: '',
                    ExtensionCondition: '',
                },
                success: function (data) {
                    if (data.length == 1) {
                        that.form = deepClone(data[0]);
                        that.OldForm = deepClone(data[0]);
                    } else {
                        that.OldForm = null;
                    }
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        getList(form) {
            const that = this
            let data = deepClone(form);
            data.Condition = window.btoa(unescape(encodeURIComponent("%" + data.Article_Name + "%")));
            let sql = "";
            if (data.Supplier_SerialNumber) {
                sql = sql + "and business_supplier_article.Supplier_SerialNumber = '" + data.Supplier_SerialNumber + "'\r\n";
            }
            if (data.Brand_SerialNumber) {
                sql = sql + "and business_article_info.Brand_SerialNumber = '" + data.Brand_SerialNumber + "'\r\n";
            }
            data.ExtensionCondition = window.btoa(unescape(encodeURIComponent(sql)));
            $.ajax({
                headers: requestHeaders(),
                type: 'POST',
                url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
                data,
                success: function (Result) {
                    Result.Rows.forEach(item => {
                        let Piont = "";
                        Piont = Piont + (Piont ? ',' : '') + (item.Gift_Point == '0' ? 'Gift' : '');
                        Piont = Piont + (Piont ? ',' : '') + (item.Cash_Point == '0' ? 'Cash' : '');
                        Piont = Piont + (Piont ? ',' : '') + (item.Sales_Point == '0' ? 'Sales' : '');
                        Piont = Piont + (Piont ? ',' : '') + (item.Activit_Point == '0' ? 'Activit' : '');
                        item.Pionts = Piont ? Piont.split(',') : [];
                        item.loading = false;
                    })
                    that.tableData = Result.Rows;
                    that.total = Result.TotalRows;
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        checkModify() {
            if (this.OldForm) {
                return this.form.Gift_Points == this.OldForm.Gift_Points &&
                    this.form.Points_Deduction == this.OldForm.Points_Deduction &&
                    this.form.Deduction_Ratio == this.OldForm.Deduction_Ratio &&
                    this.form.Deduction_Limit == this.OldForm.Deduction_Limit &&
                    this.form.Row_Status == this.OldForm.Row_Status
            } else {
                return this.form.Row_Status == '1'
            }
        },
        submitForm(formName) {
            const that = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (that.checkModify()) {
                        that.$message({
                            message: '当前设置并未改变！',
                            type: 'warning'
                        })
                        return
                    }
                    that.loading = true;
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${BASE_URL2}/generalBusiness/generalSave`,
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({ Add: { business_points_config: [that.form] } })
                        },
                        success: function (res) {
                            if (res.Status === 'success') {
                                that.$message.success("配置成功")
                                that.getData();
                                that.Refresh = true;
                            } else {
                                that.$message.error("提交失败，请稍后再试")
                            }
                        },
                        error: function () {
                            that.$message.error("操作失败，请稍后再试")
                        },
                        complete: function (XMLHttpRequest) {
                            that.loading = false
                            requestPretreatment(XMLHttpRequest);
                        }
                    })
                } else {
                    if (!that.activeNames.some(i => i == '1')) {
                        that.activeNames.push('1')
                    }
                    return false;
                }
            });
        },
        Save(row) {
            const that = this;
            let obj = {
                Points_Detail_Config_SerialNumber: '',
                Supplier_Article_SerialNumber: row.Supplier_Article_SerialNumber,
                Gift_Point: row.Pionts.some(i => i == 'Gift') ? '0' : '1',
                Sales_Point: row.Pionts.some(i => i == 'Sales') ? '0' : '1',
                Cash_Point: row.Pionts.some(i => i == 'Cash') ? '0' : '1',
                Activit_Point: row.Pionts.some(i => i == 'Activit') ? '0' : '1',
                Deduction_Type: row.Deduction_Type,
                Deduction_Limit: row.Deduction_Limit,
                Combination: row.Combination,
            }
            if (!(obj.Gift_Point == row.Gift_Point_Original && obj.Sales_Point == row.Sales_Point_Original &&
                obj.Cash_Point == row.Cash_Point_Original && obj.Activit_Point == row.Activit_Point_Original &&
                obj.Deduction_Type == row.Deduction_Type_Original && obj.Deduction_Limit == row.Deduction_Limit_Original &&
                obj.Combination == row.Combination_Original)) {
                row.loading = true;
                $.ajax({
                    headers: requestHeaders(),
                    url: `${BASE_URL2}/generalBusiness/generalSave`,
                    type: 'POST',
                    data: {
                        Data: JSON.stringify({ Add: { business_points_detail_config: [obj] } })
                    },
                    success: function (res) {
                        if (res.Status === 'success') {
                            that.$message.success("保存成功")
                            that.getList(that.page);
                        } else {
                            that.$message.error("保存失败，请稍后再试")
                        }
                    },
                    error: function () {
                        that.$message.error("操作失败，请稍后再试")
                    },
                    complete: function (XMLHttpRequest) {
                        row.loading = false
                        requestPretreatment(XMLHttpRequest);
                    }
                })
            } else {
                that.$message({
                    message: '当前设置并未改变！',
                    type: 'warning'
                })
            }
        },
        wholeSave() {
            const that = this;
            this.$confirm('是否要更当前数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                let arr = [];
                that.tableData.forEach(row => {
                    let obj = {
                        Points_Detail_Config_SerialNumber: '',
                        Supplier_Article_SerialNumber: row.Supplier_Article_SerialNumber,
                        Gift_Point: row.Pionts.some(i => i == 'Gift') ? '0' : '1',
                        Sales_Point: row.Pionts.some(i => i == 'Sales') ? '0' : '1',
                        Cash_Point: row.Pionts.some(i => i == 'Cash') ? '0' : '1',
                        Activit_Point: row.Pionts.some(i => i == 'Activit') ? '0' : '1',
                        Deduction_Type: row.Deduction_Type,
                        Deduction_Limit: row.Deduction_Limit,
                        Combination: row.Combination,
                    }
                    if (!(obj.Gift_Point == row.Gift_Point_Original && obj.Sales_Point == row.Sales_Point_Original &&
                        obj.Cash_Point == row.Cash_Point_Original && obj.Activit_Point == row.Activit_Point_Original &&
                        obj.Deduction_Type == row.Deduction_Type_Original && obj.Deduction_Limit == row.Deduction_Limit_Original &&
                        obj.Combination == row.Combination_Original)) {
                        arr.push(obj);
                    }
                })
                if (arr.length > 0) {
                    that.loading = true;
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${BASE_URL2}/generalBusiness/generalSave`,
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({ Add: { business_points_detail_config: arr } })
                        },
                        success: function (res) {
                            if (res.Status === 'success') {
                                that.$message.success("保存成功")
                                that.getList(that.page);
                            } else {
                                that.$message.error("保存失败，请稍后再试")
                            }
                        },
                        error: function () {
                            that.$message.error("操作失败，请稍后再试")
                        },
                        complete: function (XMLHttpRequest) {
                            that.loading = false;
                            requestPretreatment(XMLHttpRequest);
                        }
                    })
                } else {
                    that.$message({
                        message: '当前没有要保存的配置！',
                        type: 'warning'
                    })
                }
            }).catch(() => { });
        }
    }
})